package com.sunwayworld.module.sys.todo.callable;

import com.alibaba.fastjson.JSON;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.context.LocalContextHelper;
import com.sunwayworld.framework.executor.core.ThreadPriority;
import com.sunwayworld.framework.executor.core.TraceCallable;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;
import com.sunwayworld.framework.security.bean.LoginUser;
import com.sunwayworld.framework.utils.ObjectUtils;
import com.sunwayworld.framework.utils.ReflectionUtils;
import com.sunwayworld.framework.utils.StringUtils;
import com.sunwayworld.module.sys.menu.bean.CoreMenuBean;
import com.sunwayworld.module.sys.menu.service.CoreMenuService;
import com.sunwayworld.module.sys.todo.bean.CoreTodoConfigBean;
import com.sunwayworld.module.sys.todo.bean.CoreTodoDTO;
import com.sunwayworld.module.sys.todo.bean.CoreTodoItemDTO;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.time.LocalDateTime;

public class SelectTodoCallable extends TraceCallable<CoreTodoDTO> {
    private static final ExpressionParser parser = new SpelExpressionParser();
    private final CoreTodoConfigBean config;
    private final String href;
    private final RestJsonWrapperBean wrapper;
    private final LoginUser loginUser;
    private boolean countOnly;
    private boolean effectiveOnly;

    public SelectTodoCallable(CoreTodoConfigBean config) {
        this(config, (CoreMenuBean)((CoreMenuService)ApplicationContextHelper.getBean(CoreMenuService.class)).selectById(config.getMenuId()));
    }

    public SelectTodoCallable(CoreTodoConfigBean config, CoreMenuBean menu) {
        super(config.getId() + "[" + config.getName() + "]", ThreadPriority.BelowNormal);
        this.countOnly = true;
        this.effectiveOnly = true;
        this.config = config;
        this.loginUser = (LoginUser)ObjectUtils.clone(LocalContextHelper.getLoginPrincipal());
        this.wrapper = new RestJsonWrapperBean();
        String jsonParam = config.getJsonParam();
        if (!StringUtils.isEmpty(jsonParam)) {
            JSON.parseObject(jsonParam).forEach((k, v) -> {
                this.wrapper.setFilterValue(k, v);
            });
        }

        CoreMenuService menuService = (CoreMenuService)ApplicationContextHelper.getBean(CoreMenuService.class);
        this.href = menuService.getReplacedHref(menu);
        if ("1".equals(config.getInheritParam()) && StringUtils.contains(this.href, "?")) {
            String params = this.href.substring(this.href.indexOf("?") + 1);
            String[] var6 = StringUtils.split(params, "&");
            int var7 = var6.length;

            for(int var8 = 0; var8 < var7; ++var8) {
                String param = var6[var8];
                String[] values = StringUtils.split(param, "=");
                this.wrapper.setFilterValue(values[0], values.length > 1 ? values[1] : null);
            }
        }

    }

    public CoreTodoConfigBean getConfig() {
        return this.config;
    }

    public boolean isEffectiveOnly() {
        return this.effectiveOnly;
    }

    public void setEffectiveOnly(boolean effectiveOnly) {
        this.effectiveOnly = effectiveOnly;
    }

    public boolean isCountOnly() {
        return this.countOnly;
    }

    public void setCountOnly(boolean countOnly) {
        this.countOnly = countOnly;
    }

    public CoreTodoDTO call() {
        return new CoreTodoDTO();
    }

    private CoreTodoItemDTO parseTodoItem(Object item, String descExpression) {
        CoreTodoItemDTO todoItem = new CoreTodoItemDTO();
        todoItem.setId(ObjectUtils.toString(ReflectionUtils.getFieldValue(item, "id")));
        todoItem.setCreatedById((String)ReflectionUtils.getFieldValue(item, "createdById"));
        todoItem.setCreatedByName((String)ReflectionUtils.getFieldValue(item, "createdByName"));
        todoItem.setCreatedTime((LocalDateTime)ReflectionUtils.getFieldValue(item, "createdTime"));
        todoItem.setCreatedByOrgId((String)ReflectionUtils.getFieldValue(item, "createdByOrgId"));
        todoItem.setCreatedByOrgName((String)ReflectionUtils.getFieldValue(item, "createdByOrgName"));
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("item", item);
        Expression exp = parser.parseExpression(descExpression);
        todoItem.setDesc((String)exp.getValue(context));
        return todoItem;
    }
}
